﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.Utility;
using Jalal.Library.Data;
using System.Data.Common;
using System.Data;

namespace BCGC.DAL
{
    public class PostData
    {
        public static int CreatePost(int categoryID, int userID, string postTitle, string postContent, DateTime postDate, int tag, bool isReply, int replyTOID, int displayOrder, bool isActive, int latestPostID)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreatePost";
            dataMiner.AddParameter(command, "@CategoryID", DbType.Int32, 0, ParameterDirection.Input, categoryID);
            dataMiner.AddParameter(command, "@UserID", DbType.Int32, 0, ParameterDirection.Input, userID);
            dataMiner.AddParameter(command, "@PostTitle", DbType.String, 400, ParameterDirection.Input, postTitle);
            dataMiner.AddParameter(command, "@PostContent", DbType.String, 0, ParameterDirection.Input,postContent);
            dataMiner.AddParameter(command, "@PostDate", DbType.DateTime, 0, ParameterDirection.Input, postDate);
            if(tag==0)
                dataMiner.AddParameter(command, "@TagID", DbType.Int32, 0, ParameterDirection.Input,DBNull.Value);
            else
                dataMiner.AddParameter(command, "@TagID", DbType.Int32, 0, ParameterDirection.Input, tag);

            dataMiner.AddParameter(command, "@IsReply", DbType.Boolean, 0, ParameterDirection.Input, isReply);
            dataMiner.AddParameter(command, "@ReplyToID", DbType.Int32, 0, ParameterDirection.Input,replyTOID);
            dataMiner.AddParameter(command, "@DisplayOrder", DbType.Int32, 0, ParameterDirection.Input, displayOrder);
            dataMiner.AddParameter(command, "@IsActive", DbType.Boolean, 0, ParameterDirection.Input, isActive);
            dataMiner.AddParameter(command, "@LatestPostID", DbType.Int32, 0, ParameterDirection.Input, latestPostID);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (int)CommonUtility.FilterNull(id, typeof(int));
        }

        public static bool UpdatePost(int postID, int categoryID, int userID, string postTitle, string postContent, DateTime postDate, int tag, bool isReply, int replyTOID, int displayOrder, bool isActive, int latestPostID)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdatePost";
            dataMiner.AddParameter(command, "@PostID", DbType.Int32, 0, ParameterDirection.Input, postID);
            dataMiner.AddParameter(command, "@CategoryID", DbType.Int32, 0, ParameterDirection.Input, categoryID);
            dataMiner.AddParameter(command, "@UserID", DbType.Int32, 0, ParameterDirection.Input, userID);
            dataMiner.AddParameter(command, "@PostTitle", DbType.String, 400, ParameterDirection.Input, postTitle);
            dataMiner.AddParameter(command, "@PostContent", DbType.String, 0, ParameterDirection.Input, postContent);
            dataMiner.AddParameter(command, "@PostDate", DbType.DateTime, 0, ParameterDirection.Input, postDate);
            dataMiner.AddParameter(command, "@TagID", DbType.Int32, 0, ParameterDirection.Input, tag);
            dataMiner.AddParameter(command, "@IsReply", DbType.Boolean, 0, ParameterDirection.Input, isReply);
            dataMiner.AddParameter(command, "@ReplyToID", DbType.Int32, 0, ParameterDirection.Input, replyTOID);
            dataMiner.AddParameter(command, "@DisplayOrder", DbType.Int32, 0, ParameterDirection.Input, displayOrder);
            dataMiner.AddParameter(command, "@IsActive", DbType.Boolean, 0, ParameterDirection.Input, isActive);
            dataMiner.AddParameter(command, "@LatestPostID", DbType.Int32, 0, ParameterDirection.Input, latestPostID);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (bool)CommonUtility.FilterNull(id, typeof(bool));
        }



        public static DataTable GetPostByCategory(int categoryID)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetAllPostByCategoryID";
            dataMiner.AddParameter(command, "@CategoryID", DbType.Int32, 0, ParameterDirection.Input, categoryID);
            return dataMiner.GetListData(command);
        }

        public static DataTable GetAllPost(int? categoryId, int? userID, string tag, string sortColumn, string sortDirection, int currentPageIndex, int pageSize, bool showReply, out int totalRecords)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();

            command.CommandText = "CSP_GetAllPostPagedCount";
            dataMiner.AddParameter(command, "@CategoryID", DbType.Int32, 0, ParameterDirection.Input, categoryId);
            dataMiner.AddParameter(command, "@UserID", DbType.Int32, 0, ParameterDirection.Input, userID);
            dataMiner.AddParameter(command, "@Tag", DbType.String, 0, ParameterDirection.Input, "%" + tag + "%");
            dataMiner.AddParameter(command, "@SortColumn", DbType.String, 0, ParameterDirection.Input, sortColumn);
            dataMiner.AddParameter(command, "@SortDirection", DbType.String, 0, ParameterDirection.Input, sortDirection);
            dataMiner.AddParameter(command, "@CurrentPageIndex", DbType.Int32, 0, ParameterDirection.Input, currentPageIndex);
            dataMiner.AddParameter(command, "@PageSize", DbType.Int32, 0, ParameterDirection.Input, pageSize);
            dataMiner.AddParameter(command, "@ShowReply", DbType.Boolean, 0, ParameterDirection.Input, showReply);
            totalRecords = Convert.ToInt32(dataMiner.GetOneData(command));
            totalRecords = (int)CommonUtility.FilterNull(totalRecords, typeof(int));

            command.CommandText = "CSP_GetAllPostPaged";
           
            return dataMiner.GetListData(command);
        }

        public static DataTable GetPostByPostID(int postID)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetPostByPostID";
            dataMiner.AddParameter(command, "@PostID", DbType.Int32, 0, ParameterDirection.Input, postID);
            return dataMiner.GetListData(command);
        }

        public static DataTable GetPostDetailsByPostID(int postID)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetPostDetails";
            dataMiner.AddParameter(command, "@PostID", DbType.Int32, 0, ParameterDirection.Input, postID);
            return dataMiner.GetListData(command);
        }

        public static bool DeactivatePost(int postID)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_DeactivatePost";
            dataMiner.AddParameter(command, "@PostID", DbType.Int32, 0, ParameterDirection.Input, postID);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (bool)CommonUtility.FilterNull(id, typeof(bool));

        }

        public static bool DeletePost(int postID)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_DeletePost";
            dataMiner.AddParameter(command, "@PostID", DbType.Int32, 0, ParameterDirection.Input, postID);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (bool)CommonUtility.FilterNull(id, typeof(bool));

        }

        public static DataTable GetPostListingByCategory(int categoryId, string sortBy, string sortDirection, int currentPage, int pageSize, out int totalCount)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetPostListingByCategoryPaged";
            dataMiner.AddParameter(command, "@CategoryID", DbType.Int32, 0, ParameterDirection.Input, categoryId);
            dataMiner.AddParameter(command, "@SortColumn", DbType.String, 0, ParameterDirection.Input, sortBy);
            dataMiner.AddParameter(command, "@SortDirection", DbType.String, 0, ParameterDirection.Input, sortDirection);
            dataMiner.AddParameter(command, "@CurrentPageIndex", DbType.Int32, 0, ParameterDirection.Input, currentPage);
            dataMiner.AddParameter(command, "@PageSize", DbType.Int32, 0, ParameterDirection.Input, pageSize);
            totalCount = 0;
            return dataMiner.GetListData(command);
        }

        public static DataTable GetAllLatestPost(int categoryId, string sortColumn, string sortDirection, int currentPage, int pazeSize, out int totalCount)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
           
            dataMiner.AddParameter(command, "@CategoryID", DbType.Int32, 0, ParameterDirection.Input, categoryId);
            dataMiner.AddParameter(command, "@SortColumn", DbType.String, 0, ParameterDirection.Input, sortColumn);
            dataMiner.AddParameter(command, "@SortDirection", DbType.String, 0, ParameterDirection.Input, sortDirection);
            dataMiner.AddParameter(command, "@CurrentPageIndex", DbType.Int32, 0, ParameterDirection.Input, currentPage);
            dataMiner.AddParameter(command, "@PageSize", DbType.Int32, 0, ParameterDirection.Input, pazeSize);

            command.CommandText = "CSP_GetAllRecentPostPagedCount";
            totalCount = Convert.ToInt32(dataMiner.GetOneData(command));
            totalCount = (int)CommonUtility.FilterNull(totalCount, typeof(int));

            command.CommandText = "CSP_GetAllRecentPostPaged";          
            return dataMiner.GetListData(command);
        }

        public static bool UpdateLatestPost(int postID, int latestPostID)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdateLatestPost";
            dataMiner.AddParameter(command, "@PostID", DbType.Int32, 0, ParameterDirection.Input, postID);
            dataMiner.AddParameter(command, "@LatestPostID", DbType.Int32, 0, ParameterDirection.Input, latestPostID);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);
            int result=(int)CommonUtility.FilterNull(id, typeof(int));
            return (result > 0 ? true : false);
        }
    }
}
